home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: Does C convert float to double internally ?
- Date: 4 Apr 96 19:30:41 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.828646241@rscernix>
- References: <4jsllh$hkf@harbinger.cc.monash.edu.au> <4jv2ho$r1o@harbinger.cc.monash.edu.au>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4jv2ho$r1o@harbinger.cc.monash.edu.au> bcheung@yoyo.cc.monash.edu.au (Biggles Cheung) writes:
-
- >Biggles Cheung (bcheung@yoyo.cc.monash.edu.au) wrote:
- >: I was told that C compiler automatically converts "float" variable
- >: internally to "double" for + - * / , etc operations. Is this true?
- >
- >After I have read some books on C/C++, they all say that K&R C used to
- >do _implicit_ conversion from float to double both in unary and binary
- >operations, while ANSI C has dropped the implicit conversion and happily
- >allows float to be float at all time.
-
- The keyword here is "allows". ANSI C doesn't _require_ an operation
- involving only float operands to be carried on in single precision.
-
- >Can I safely assume now that ALL current C/C++ compliers are following ANSI C
- >standard?
-
- Almost, but this doesn't solve your problem. See above.
- >:
- >: Can I force the C compiler not to do the conversion as I have an
- >: assignment on finding out various computing errors of different
- >: precisions?
- >:
- >I have read also the Turbo C++ manual and it says "tcc -ff- " command
- >line option can force the command line compiler to follow STRICT ANSI C
- >rule on conversion, i.e. no implicit conversion.
-
- There is no such rule in ANSI C (that I know of). See above.
-
- >Can I now be sure that if I use "tcc -ff- program.c", there is _no_
- >implicit conversion? But what about the IDE interface?
-
- Compiler specific questions do _not_ belong to this newsgroup. We are
- discussing the language, not one compiler or another. Try a platform
- specific newsgroup where that compiler is relevant (e.g.
- comp.os.msdos.programmer).
-
- >Can someone suggest a simple program to test if there is any implicit
- >conversion happening behind the curtain?
-
- Such a program cannot be written in a portable way. An implementation
- where float and double are identical is perfectly legal, as long as the
- minimal requirements for precision and range for the type double are met.
- >
- >: My advice from the lecturer is to have temporary variable of
- >: float to split a long equation to smaller step so that I can maximize
- >: the impact of single precision number on the evaluation. Is this
- >: feasible?
- >
- >If I am to find the single precision value of say, 12.3457 to the power
- >of 13 by multiplying 12.3456 thirteen times, does that mean I have to
- >store the result of every _single_ multiplication to a temporary variable
- >of float to enforce the single precision?
-
- Yes.
-
- >What if I have a very complicated mathematical expression like Taylor's
- >series plus sqrt plus more ... ? How many temporary variables I need and
- >when is the best time or where is the best spot to insert such
- >_nuisance_?
-
- As many as needed. The idea is to assign the result of _any_ operation
- to a float, to be sure that no calculation is done with more bits of
- precision than you actually want/need.
-
- For most intents and purposes, there is no need to force the precision
- of the intermediary results to float. The idea behind the current
- definition of the language is to allow the implementor the flexibility
- to choose the most efficient way to perform the arithmetic on a given
- platform. There are processors which have only double precision FPU's
- and forcing single precision for the intermediary results would mean
- a lot of conversions, which would waste precious cpu cycles.
-
- >_/\/\/\/\/\___/\/\_________________________/\/\_________________________
- >_/\/\____/\/\__________/\/\/\/\___/\/\/\/\_/\/\_____/\/\/\_____/\/\/\/\_
- >_/\/\/\/\/\___/\/\___/\/\__/\/\_/\/\__/\/\_/\/\___/\/\/\/\/\_/\/\/\/\___
- >_/\/\____/\/\_/\/\_____/\/\/\/\___/\/\/\/\_/\/\___/\/\_____________/\/\_
- >_/\/\/\/\/\___/\/\/\_______/\/\_______/\/\_/\/\/\___/\/\/\/\_/\/\/\/\___
- >____________________/\/\/\/\___/\/\/\/\_________________________________
- >
- > / biggles@c031.aone.net.au \
- > \ bcheung@yoyo.cc.monash.edu.au / ~~ Biggles CHEUNG
-
- Please trim your signature and retain only the last two lines. The others
- don't carry any bit of useful information and are a pure waste of
- bandwidth.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-